package band.wukong.practice.netty.sample.tcpsuite;

import band.wukong.practice.netty.Env;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * Desc:
 * author: wukonggg@139.com
 */
public class TcpClient {

    static {
        Env.assemblingLog4j2ConfFile();
    }

    public void connect(String host, int port) throws Exception {
        EventLoopGroup g = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(g).channel(NioSocketChannel.class)
                    .option(ChannelOption.TCP_NODELAY, true)
                    .handler(new TcpClientChannelInitializer());

            ChannelFuture f = b.connect(host, port).sync();
            f.channel().closeFuture().sync();

        } finally {
            g.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        new TcpClient().connect(Env.HOST, Env.PORT);
    }
}
